﻿
using DevComponents.DotNetBar;
using Elike.Toolkit.ControlsErgodic.Windows;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using shopsoft.models.EnumObject;
using shopsoft.models;
using System.Drawing.Imaging;
using System.IO;

namespace shopsoft.main.AppCommon
{
    public class WfControl
    {
        #region Method

        /// <summary>
        /// 验证失败后退出系统
        /// </summary>
        /// <param name="message">提示信息（注：提示信息为null或空字符串时，则不弹出提示框，退出系统）</param>
        public void ReLogin(string message)
        {
            if (message != null && message.Trim() != string.Empty)
            {
                MsgBox.Warning("提示", message);
            }
            Application.Exit();
        }

        /// <summary>
        /// 删除DataGridView中DataTable的行
        /// </summary>
        /// <param name="ids">要删除的行的id列表</param>
        /// <param name="splitChar">分隔符</param>
        /// <param name="idColumnName">id列的列名</param>
        /// <param name="dataGridView">DataGridView对象</param>
        public void DeleteDataTableRows(string ids, char splitChar, string idColumnName, DataGridView dataGridView)
        {
            for (int rowIndex = dataGridView.Rows.Count - 1; rowIndex >= 0; rowIndex--)
            {
                var checkCell = dataGridView.Rows[rowIndex].Cells[idColumnName];
                if (checkCell != null && ids.Split(splitChar).Contains(checkCell.EditedFormattedValue))
                {
                    dataGridView.Rows.RemoveAt(rowIndex);
                }
            }
        }

        /// <summary>
        /// 设置操作项的显示
        /// </summary> 
        /// <param name="permissionTypes">权限类型列表</param>
        /// <param name="splitChar">分隔符</param>
        /// <param name="bar">菜单栏</param>
        public void SetOperationItemVisible(List<PermissionType> permissionTypes, char splitChar, DevComponents.DotNetBar.Bar bar)
        {
            //var pTtypes = permissionTypes.Split(splitChar).ToList();
            //var bar = (Bar)form.Controls.Find("bar1", true)[0];
            if (permissionTypes.Count > 0)
            {
                bar.Visible = true;
                foreach (var permissionType in permissionTypes)
                {
                    for (int i = 0; i < bar.Items.Count; i++)
                    {
                        if (bar.Items[i].Name == "buttonItem" + (int)permissionType.EPermissionType)
                        {
                            var buttonItem = bar.Items["buttonItem" + (int)permissionType.EPermissionType] as ButtonItem;
                            if (buttonItem != null)
                            {
                                buttonItem.Visible = true;
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                bar.Visible = false;
            }
        }

        /// <summary>
        /// 设置操作项的显示
        /// </summary> 
        /// <param name="permissionTypes">权限类型列表</param>
        /// <param name="splitChar">分隔符</param>
        /// <param name="dataGridView">DataGridView对象</param>
        public void SetOperationItemVisible(string permissionTypes, char splitChar, DataGridView dataGridView)
        {
            var pTtypes = permissionTypes.Split(splitChar).ToList();
            var bar = (Bar)dataGridView.FindForm().Controls.Find("bar1", true)[0];
            if (pTtypes.Count > 0)
            {
                bar.Visible = true;
                foreach (var pType in pTtypes)
                {
                    for (int i = 0; i < bar.Items.Count; i++)
                    {
                        if (bar.Items[i].Name == "buttonItem" + pType)
                        {
                            var buttonItem = bar.Items["buttonItem" + pType] as ButtonItem;
                            if (buttonItem != null)
                            {
                                buttonItem.Visible = true;
                                break;
                            }
                        }
                    }
                }
                //设置CheckBox单选列是否显示
                if (dataGridView.Columns.Contains("Selected"))
                {
                    dataGridView.Columns["Selected"].Visible = pTtypes.Contains(((int)EPermissionType.Delete).ToString())
                                                               || pTtypes.Contains(((int)EPermissionType.Recover).ToString())
                                                               || pTtypes.Contains(((int)EPermissionType.Export).ToString());
                }
                //设置修改按扭的Image列是否显示
                if (dataGridView.Columns.Contains("btnUpdate"))
                {
                    dataGridView.Columns["btnUpdate"].Visible = pTtypes.Contains(((int)EPermissionType.Update).ToString());
                }
                //设置重置密码按扭的Image列是否显示
                if (dataGridView.Columns.Contains("BtnPassWord"))
                {
                    dataGridView.Columns["BtnPassWord"].Visible = pTtypes.Contains(((int)EPermissionType.Update).ToString());
                }
                //设置照片管理按扭的Image列是否显示
                if (dataGridView.Columns.Contains("btnPhoto"))
                {
                    dataGridView.Columns["btnPhoto"].Visible = pTtypes.Contains(((int)EPermissionType.Update).ToString());
                }
            }
            else
            {
                bar.Visible = false;
            }
        }

        /// <summary>
        /// 设置操作项的显示
        /// </summary> 
        /// <param name="permissionTypes">权限类型列表</param>
        /// <param name="splitChar">分隔符</param>
        /// <param name="dataGridView">DataGridView对象</param>
        public void SetOperationItemVisible(List<PermissionType> permissionTypes, char splitChar, DataGridView dataGridView)
        {
            //var pTtypes = permissionTypes.Split(splitChar).ToList();
            var bar = (Bar)dataGridView.FindForm().Controls.Find("bar1", true)[0];
            if (permissionTypes.Count > 0)
            {
                bar.Visible = true;
                foreach (var permissionType in permissionTypes)
                {
                    for (int i = 0; i < bar.Items.Count; i++)
                    {
                        if (bar.Items[i].Name == "buttonItem" + (int)permissionType.EPermissionType)
                        {
                            var buttonItem = bar.Items["buttonItem" + (int)permissionType.EPermissionType] as ButtonItem;
                            if (buttonItem != null)
                            {
                                buttonItem.Visible = true;
                                break;
                            }
                        }
                    }
                }
                //设置CheckBox单选列是否显示
                if (dataGridView.Columns.Contains("Selected"))
                {
                    var dataGridViewColumn = dataGridView.Columns["Selected"];
                    if (dataGridViewColumn != null)
                        dataGridViewColumn.Visible =
                            permissionTypes.Any(c => c.EPermissionType == EPermissionType.Delete || c.EPermissionType == EPermissionType.Recover ||
                                                     c.EPermissionType == EPermissionType.Export);
                }
                //设置修改按扭的Image列是否显示
                if (dataGridView.Columns.Contains("btnUpdate"))
                {
                    var dataGridViewColumn = dataGridView.Columns["btnUpdate"];
                    if (dataGridViewColumn != null)
                        dataGridViewColumn.Visible =
                            permissionTypes.Any(c => c.EPermissionType == EPermissionType.Update);
                }
                //设置重置密码按扭的Image列是否显示
                if (dataGridView.Columns.Contains("BtnPassWord"))
                {
                    var dataGridViewColumn = dataGridView.Columns["BtnPassWord"];
                    if (dataGridViewColumn != null)
                        dataGridViewColumn.Visible =
                            permissionTypes.Any(c => c.EPermissionType == EPermissionType.Update);
                }
                //设置上传文档按扭的Image列是否显示
                if (dataGridView.Columns.Contains("btnUpReport"))
                {
                    var dataGridViewColumn = dataGridView.Columns["btnUpReport"];
                    if (dataGridViewColumn != null)
                        dataGridViewColumn.Visible =
                            permissionTypes.Any(c => c.EPermissionType == EPermissionType.Update);
                }
            }
            else
            {
                bar.Visible = false;
                //设置CheckBox单选列是否显示
                if (dataGridView.Columns.Contains("Selected"))
                {
                    var dataGridViewColumn = dataGridView.Columns["Selected"];
                    if (dataGridViewColumn != null)
                        dataGridViewColumn.Visible = false;
                }
                //设置修改按扭的Image列是否显示
                if (dataGridView.Columns.Contains("btnUpdate"))
                {
                    var dataGridViewColumn = dataGridView.Columns["btnUpdate"];
                    if (dataGridViewColumn != null)
                        dataGridViewColumn.Visible = false;
                }
                //设置重置密码按扭的Image列是否显示
                if (dataGridView.Columns.Contains("BtnPassWord"))
                {
                    var dataGridViewColumn = dataGridView.Columns["BtnPassWord"];
                    if (dataGridViewColumn != null)
                        dataGridViewColumn.Visible = false;
                }
                //设置上传文档按扭的Image列是否显示
                if (dataGridView.Columns.Contains("btnUpReport"))
                {
                    var dataGridViewColumn = dataGridView.Columns["btnUpReport"];
                    if (dataGridViewColumn != null)
                        dataGridViewColumn.Visible = false;
                }
            }
        }

        /// <summary>
        /// 设置操作项的显示
        /// </summary> 
        /// <param name="permissionTypes">权限类型列表</param>
        /// <param name="splitChar">分隔符</param>
        /// <param name="listView">ListView对象</param>
        public void SetOperationItemVisible(string permissionTypes, char splitChar, ListView listView)
        {
            List<string> pTtypes = permissionTypes.Split(splitChar).ToList();
            var bar = (Bar)listView.FindForm().Controls.Find("bar1", true)[0];
            if (pTtypes.Count > 0)
            {
                bar.Visible = true;
                foreach (var pType in pTtypes)
                {
                    //Bar中的功能按钮
                    for (int i = 0; i < bar.Items.Count; i++)
                    {
                        if (bar.Items[i].Name == "buttonItem" + pType)
                        {
                            var buttonItem = bar.Items["buttonItem" + pType] as ButtonItem;
                            if (buttonItem != null)
                            {
                                buttonItem.Visible = true;
                                break;
                            }
                        }
                    }
                    if (listView.ContextMenuStrip != null)
                    {
                        for (int i = 0; i < listView.ContextMenuStrip.Items.Count; i++)
                        {
                            //快捷菜单中的功能按钮
                            if (listView.ContextMenuStrip.Items[i].Name == "buttonItem" + pType)
                            {
                                listView.ContextMenuStrip.Items[i].Visible = true;
                                break;
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 设置操作项的显示
        /// </summary> 
        /// <param name="permissionTypes">权限类型列表</param>
        /// <param name="splitChar">分隔符</param>
        /// <param name="listView">ListView对象</param>
        public void SetOperationItemVisible(List<PermissionType> permissionTypes, char splitChar, ListView listView)
        {
            var bar = (Bar)listView.FindForm().Controls.Find("bar1", true)[0];
            if (permissionTypes.Count > 0)
            {
                bar.Visible = true;
                foreach (var permissionType in permissionTypes)
                {
                    //Bar中的功能按钮
                    for (int i = 0; i < bar.Items.Count; i++)
                    {
                        if (bar.Items[i].Name == "buttonItem" + (int)permissionType.EPermissionType)
                        {
                            var buttonItem = bar.Items["buttonItem" + (int)permissionType.EPermissionType] as ButtonItem;
                            if (buttonItem != null)
                            {
                                buttonItem.Visible = true;
                                break;
                            }
                        }
                    }
                    if (listView.ContextMenuStrip != null)
                    {
                        for (int i = 0; i < listView.ContextMenuStrip.Items.Count; i++)
                        {
                            //快捷菜单中的功能按钮
                            if (listView.ContextMenuStrip.Items[i].Name == "buttonItem" + (int)permissionType.EPermissionType)
                            {
                                listView.ContextMenuStrip.Items[i].Visible = true;
                                break;
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 设置操作项的可用性
        /// </summary>
        /// <param name="dataGridView">DataGridView对象</param>
        /// <param name="rowIndex">被点击的行索引号</param>
        /// <param name="checkedCount">操作前已被选中项的数量</param>
        /// <param name="displayFirstColumnIndex">显示数据的第一列的列索引（用于CheckBox列编辑后改变焦点列）</param>
        /// <returns>操作后已被中项的数量</returns>
        public int SetMenuOperationItemEnable(DataGridView dataGridView, int rowIndex, int checkedCount, int displayFirstColumnIndex)
        {
            if (rowIndex != -1 && rowIndex != dataGridView.Rows.Count + 1)
            {
                var checkCell = dataGridView.Rows[rowIndex].Cells[0] as DataGridViewCheckBoxCell;

                if (checkCell != null && !checkCell.ReadOnly)
                {
                    checkedCount = (bool)checkCell.EditedFormattedValue ? checkedCount + 1 : checkedCount - 1;
                    checkedCount = checkedCount <= 0 ? 0 : checkedCount;
                    var contextMenuStrip = dataGridView.ContextMenuStrip;
                    ToolStripItem buttonItem;
                    var commands = new List<string>()
                        {
                            ((int) EPermissionType.Delete).ToString(),
                            ((int) EPermissionType.Recover).ToString(),
                            ((int) EPermissionType.Export).ToString(),
                            ((int) EPermissionType.Import).ToString()
                        };

                    for (int i = 0; i < contextMenuStrip.Items.Count; i++)
                    {
                        if (commands.Contains(contextMenuStrip.Items[i].Name.Replace("buttonItem", "")))
                        {
                            buttonItem = contextMenuStrip.Items[i] as ToolStripItem;
                            if (buttonItem != null)
                            {
                                buttonItem.Enabled = checkedCount > 0;
                            }
                        }
                    }
                }
            }
            if (dataGridView.CurrentCell.ColumnIndex == 0)
            {
                dataGridView.CurrentCell = dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[displayFirstColumnIndex];
            }
            return checkedCount;
        }

        /// <summary>
        /// 获取指定DataGridView中指定行指定列中的CheckBox的选中状态
        /// </summary>
        /// <param name="dataGridView">DataGridView对象</param>
        /// <param name="rowIndex">行索引</param>
        /// <param name="checkBoxColumnIndex">CheckBox列索引</param>
        /// <returns></returns>
        public bool GetRowIsChecked(DataGridView dataGridView, int rowIndex, int checkBoxColumnIndex)
        {
            bool isChecked = false;
            var checkCell = dataGridView.Rows[rowIndex].Cells[checkBoxColumnIndex] as DataGridViewCheckBoxCell;
            if (checkCell != null && !checkCell.ReadOnly)
            {
                isChecked = (bool)checkCell.EditedFormattedValue;
            }
            return isChecked;
        }

        /// <summary>
        /// 设置操作项的可用性
        /// </summary>
        /// <param name="dataGridView">DataGridView对象</param>
        /// <param name="rowIndex">被点击的行索引号</param>
        /// <param name="checkedCount">操作前已被选中项的数量</param>
        /// <returns>操作后已被中项的数量</returns>
        public int SetOperationItemEnable(DataGridView dataGridView, int rowIndex, int checkedCount)
        {
            return SetOperationItemEnable(dataGridView, rowIndex, checkedCount, 1);
        }

        /// <summary>
        /// 设置操作项的可用性
        /// </summary>
        /// <param name="dataGridView">DataGridView对象</param>
        /// <param name="rowIndex">被点击的行索引号</param>
        /// <param name="checkedCount">操作前已被选中项的数量</param>
        /// <param name="displayFirstColumnIndex">显示数据的第一列的列索引（用于CheckBox列编辑后改变焦点列）</param>
        /// <returns>操作后已被中项的数量</returns>
        public int SetOperationItemEnable(DataGridView dataGridView, int rowIndex, int checkedCount, int displayFirstColumnIndex)
        {
            if (rowIndex != -1 && rowIndex != dataGridView.Rows.Count + 1)
            {
                var checkCell = dataGridView.Rows[rowIndex].Cells[0] as DataGridViewCheckBoxCell;

                if (checkCell != null && !checkCell.ReadOnly)
                {
                    checkedCount = (bool)checkCell.EditedFormattedValue ? checkedCount + 1 : checkedCount - 1;
                    checkedCount = checkedCount <= 0 ? 0 : checkedCount;
                    var bar = (Bar)dataGridView.FindForm().Controls.Find("bar1", true)[0];
                    ButtonItem buttonItem;
                    var commands = new List<string>()
                    {
                        ((int) EPermissionType.Delete).ToString(),
                        ((int) EPermissionType.Recover).ToString(),
                        //((int) EPermissionType.Export).ToString()
                    };
                    for (int i = 0; i < bar.Items.Count; i++)
                    {
                        if (commands.Contains(bar.Items[i].Name.Replace("buttonItem", "")))
                        {
                            buttonItem = bar.Items[i] as ButtonItem;
                            if (buttonItem != null)
                            {
                                buttonItem.Enabled = checkedCount > 0;
                            }
                        }
                    }
                }
            }
            if (dataGridView.CurrentCell.ColumnIndex == 0)
            {
                dataGridView.CurrentCell = dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[displayFirstColumnIndex];
            }
            return checkedCount;
        }

        /// <summary>
        /// 设置操作项的可用性
        /// </summary>
        /// <param name="bar">Bar对象</param>
        /// <param name="checkedCount">操作前已被选中项的数量</param>
        public void SetOperationItemEnable(Bar bar, int checkedCount)
        {
            ButtonItem buttonItem;
            var commands = new List<string>()
            {
                ((int) EPermissionType.Delete).ToString(),
                ((int) EPermissionType.Recover).ToString(),
                //((int) EPermissionType.Export).ToString(),
                ((int) EPermissionType.Import).ToString()
            };
            var buttonItemNames = new List<string>() { "btnSaveExcel", "btnSetting", "btnDeleteRecord" };
            for (int i = 0; i < bar.Items.Count; i++)
            {
                if (commands.Contains(bar.Items[i].Name.Replace("buttonItem", "")))
                {
                    buttonItem = bar.Items[i] as ButtonItem;
                    if (buttonItem != null)
                    {
                        buttonItem.Enabled = checkedCount > 0;
                    }
                }
                else if (buttonItemNames.Contains(bar.Items[i].Name))
                {
                    buttonItem = bar.Items[i] as ButtonItem;
                    if (buttonItem != null)
                    {
                        buttonItem.Enabled = checkedCount > 0;
                    }
                }
            }
        }

        /// <summary>
        /// 全选DataGridView中的所有数据
        /// </summary>
        /// <param name="dataGridView">DataGridView对象</param>
        /// <param name="displayFirstColumnIndex">显示数据的第一列的列索引（用于CheckBox列编辑后改变焦点列）</param>
        /// <returns>操作后已被中项的数量</returns>
        public int SetAllSelected(DataGridView dataGridView, int displayFirstColumnIndex)
        {
            for (int i = 0; i < dataGridView.Rows.Count; i++)
            {
                var checkCell = dataGridView.Rows[i].Cells[0] as DataGridViewCheckBoxCell;
                if (checkCell != null)
                {
                    checkCell.Value = true;
                }
            }
            if (dataGridView.CurrentCell.ColumnIndex == 0)
            {
                dataGridView.CurrentCell = dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[displayFirstColumnIndex];
            }
            return dataGridView.Rows.Count;
        }

        /// <summary>
        /// 反选DataGridView中的数据
        /// </summary>
        /// <param name="dataGridView">DataGridView对象</param>
        /// <param name="displayFirstColumnIndex">显示数据的第一列的列索引（用于CheckBox列编辑后改变焦点列）（用于CheckBox列编辑后改变焦点列）</param>
        /// <returns>操作后已被中项的数量</returns>
        public int SetReverseSelected(DataGridView dataGridView, int displayFirstColumnIndex)
        {
            int selectedCount = 0;
            for (int i = 0; i < dataGridView.Rows.Count; i++)
            {
                var checkCell = dataGridView.Rows[i].Cells[0] as DataGridViewCheckBoxCell;
                if (checkCell != null)
                {
                    checkCell.Value = !(bool)checkCell.EditedFormattedValue;
                    if ((bool)checkCell.EditedFormattedValue)
                    {
                        selectedCount++;
                    }
                }
            }
            if (dataGridView.CurrentCell.ColumnIndex == 0)
            {
                dataGridView.CurrentCell = dataGridView.Rows[dataGridView.CurrentCell.RowIndex].Cells[displayFirstColumnIndex];
            }
            return selectedCount;
        }

        /// <summary>
        /// 获取被选中行中主键数据集（默认主键列索引为最后一列）
        /// </summary>
        /// <param name="dataGridView">DataGridView对象</param>
        /// <param name="splitChar">分隔符</param>
        /// <returns></returns>
        public string GetCheckedPrimaryDatas(DataGridView dataGridView, char splitChar)
        {
            return GetCheckedPrimaryDatas(dataGridView, splitChar, dataGridView.Columns.Count - 1);
        }

        /// <summary>
        /// 获取被选中行中主键数据集
        /// </summary>
        /// <param name="dataGridView">DataGridView对象</param>
        /// <param name="splitChar">分隔符</param>
        /// <param name="primaryColumnIndex">主键列索引</param>
        /// <returns></returns>
        public string GetCheckedPrimaryDatas(DataGridView dataGridView, char splitChar, int primaryColumnIndex)
        {
            string checkedDatas = "";
            if (primaryColumnIndex != -1)
            {
                for (int i = 0; i < dataGridView.Rows.Count; i++)
                {
                    if (GetRowIsChecked(dataGridView, i, 0))
                    {
                        checkedDatas += dataGridView.Rows[i].Cells[primaryColumnIndex].Value.ToString() + splitChar.ToString();
                    }
                }
                checkedDatas = checkedDatas.TrimEnd(splitChar);
            }
            else
            {
                throw new Exception(dataGridView.Name + "中的暂无任何绑定列");
            }
            return checkedDatas;
        }

        /// <summary>
        /// 获取被选中行的指定列的数据集
        /// </summary>
        /// <param name="dataGridView">DataGridView对象</param>
        /// <param name="columnName">列名称</param>
        /// <param name="splitChar">分隔符</param>
        /// <returns></returns>
        public string GetCheckedDatas(DataGridView dataGridView, string columnName, char splitChar)
        {
            string checkedDatas = "";
            if (dataGridView.Columns[columnName] != null)
            {
                for (int i = 0; i < dataGridView.Rows.Count; i++)
                {
                    if (GetRowIsChecked(dataGridView, i, 0))
                    {
                        checkedDatas += dataGridView.Rows[i].Cells[columnName].Value.ToString() + splitChar.ToString();
                    }
                }
                checkedDatas = checkedDatas.TrimEnd(splitChar);
            }
            else
            {
                throw new Exception(dataGridView.Name + "中的" + columnName + "列不存在");
            }
            return checkedDatas;
        }

        /// <summary>
        /// 获取被选中行的行索引（注：仅提供给修改操作使用，获取被选中行集中的第一个的行索引）
        /// </summary>
        /// <param name="dataGridView">DataGridView对象</param>
        /// <returns></returns>
        public int GetCheckedRowIndex(DataGridView dataGridView)
        {
            int rowIndex = -1;
            for (int i = 0; i < dataGridView.Rows.Count; i++)
            {
                if (GetRowIsChecked(dataGridView, i, 0))
                {
                    rowIndex = i;
                    break;
                }
            }
            return rowIndex;
        }

        /// <summary>
        /// 判断指定数据中指定列的值是否等于所指定的验证值
        /// </summary>
        /// <param name="dataGridView">DataGridView对象</param>
        /// <param name="columnName">列名称</param>
        /// <param name="validateValue">验证值</param>
        /// <returns></returns>
        public bool IsValidateValueOfColumn(DataGridView dataGridView, string columnName, string validateValue)
        {
            bool flag = true;
            if (dataGridView.Columns[columnName] != null)
            {
                for (int i = 0; i < dataGridView.Rows.Count; i++)
                {
                    if (dataGridView.Rows[i].Cells[columnName].Value.ToString() == validateValue)
                    {
                        flag = false;
                        break;
                    }
                }
            }
            else
            {
                throw new Exception(dataGridView.Name + "中的" + columnName + "列不存在");
            }
            return flag;
        }

        /// <summary>
        /// 获取ItemPanel中的CheckBoxItem是否有被选中项
        /// </summary>
        /// <param name="panel">ItemPanel对象</param>
        /// <returns></returns>
        public bool IsChecked(ItemPanel panel)
        {
            bool flag = false;
            foreach (BaseItem item in panel.Items)
            {
                if (item as CheckBoxItem != null)
                {
                    if (((CheckBoxItem)item).Checked)
                    {
                        flag = true;
                    }
                }
            }
            return flag;
        }
        /// <summary>
        /// 获取ItemPanel中的CheckBoxItem指定选中状态的Name序号集
        /// </summary>
        /// <param name="panel">ItemPanel对象</param>
        /// <param name="isChecked">是否被选中</param>
        /// <param name="sameNameString">相同名称字符串</param>
        /// <returns></returns>
        public string GetCheckedName(ItemPanel panel, bool isChecked, string sameNameString)
        {
            string nos = string.Empty;
            foreach (BaseItem item in panel.Items)
            {
                if (item as CheckBoxItem != null)
                {
                    if (((CheckBoxItem)item).Checked == isChecked)
                    {
                        nos += item.Name.Replace(sameNameString, string.Empty) + ",";
                    }
                }
            }
            return nos.TrimEnd(',');
        }

        /// <summary>
        /// 设置ItemPanel中的CheckBoxItem指定Name序号集的选中状态
        /// </summary>
        /// <param name="panel">ItemPanel对象</param>
        /// <param name="isChecked">是否被选中</param>
        /// <param name="sameNameString">相同名称字符串</param>
        /// <param name="checkBoxItemNameNOs">CheckBoxItem的Name序号集</param>
        public void SetCheckedValues(ItemPanel panel, bool isChecked, string sameNameString, string[] checkBoxItemNameNOs)
        {
            foreach (var no in checkBoxItemNameNOs)
            {
                for (int i = 0; i < panel.Items.Count; i++)
                {
                    if (panel.Items[i] as CheckBoxItem != null)
                    {
                        if (panel.Items[i].Name == sameNameString + no)
                        {
                            ((CheckBoxItem)panel.Items[i]).Checked = isChecked;
                            break;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 设置ItemPanel中的CheckBoxItem指定Name序号集的选中状态与可用性
        /// </summary>
        /// <param name="panel">ItemPanel对象</param>
        /// <param name="isChecked">是否被选中</param>
        /// <param name="isEnabled">是否可用</param>
        /// <param name="sameNameString">相同名称字符串</param>
        /// <param name="checkBoxItemNameNOs">CheckBoxItem的Name序号集</param>
        public void SetCheckedEnabledValues(ItemPanel panel, bool isChecked, bool isEnabled, string sameNameString, string[] checkBoxItemNameNOs)
        {
            foreach (var no in checkBoxItemNameNOs)
            {
                for (int i = 0; i < panel.Items.Count; i++)
                {
                    if (panel.Items[i] as CheckBoxItem != null)
                    {
                        if (panel.Items[i].Name == sameNameString + no)
                        {
                            ((CheckBoxItem)panel.Items[i]).Checked = isChecked;
                            panel.Items[i].Enabled = isEnabled;
                            break;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 获取当前被选中Button按钮项（复杂多层的ItemPanel，且选项集中仅单选项情况下使用）
        /// </summary>
        /// <param name="subItemsCollection">ItemPanel中的任意Items集</param>
        /// <param name="excludeItem">排除项（之前被选中项）</param>
        /// <returns></returns>
        public ButtonItem GetCurrentCheckedBox(SubItemsCollection subItemsCollection, ButtonItem excludeItem)
        {
            foreach (BaseItem item in subItemsCollection)
            {
                if (item is ButtonItem && item != excludeItem && ((ButtonItem)item).Checked)
                    return item as ButtonItem;
                else if (item.IsContainer)
                {
                    ButtonItem buttonItem = GetCurrentCheckedBox(item.SubItems, excludeItem);
                    if (buttonItem != null)
                        return buttonItem;
                }
            }

            return null;
        }

        /// <summary>
        /// 关闭已打开窗体中的指定窗体
        /// </summary>
        /// <param name="openWindowStartIndex">指定搜寻窗体的起始索引</param>
        /// <param name="windowClassPath">窗体类的路径字符串（可传输部分路径）</param>
        /// <returns></returns>
        public bool CloseWindow(uint openWindowStartIndex, string windowClassPath)
        {
            bool flag = false;
            for (uint i = openWindowStartIndex; i < Application.OpenForms.Count; i++)
            {
                if (Application.OpenForms[(int)i].GetType().UnderlyingSystemType.FullName.Contains(windowClassPath))
                {
                    Application.OpenForms[(int)i].Close();
                    flag = true;
                    break;
                }
            }
            return flag;
        }

        /// <summary>
        /// 解决打开含有DataGridView控件的窗口时报创建窗口句柄错误
        /// </summary>
        /// <param name="dataGridView">DataGridView对象</param>
        public void SolveCreateHandleError(DataGridView dataGridView)
        {
            //dataGridView.SuspendLayout();
            //dataGridView.ResumeLayout();
        }

        /// <summary>
        /// 设置Winform控件的双缓存机制（防闪烁）
        /// </summary>
        /// <param name="control">Winform控件</param>
        /// <param name="setting">设置值</param>
        public void SetDoubleBuffered(Control control, bool setting)
        {
            Type dgvType = control.GetType();
            PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(control, setting, null);
        }

        /// <summary>
        /// 设置系统气泡信息
        /// </summary>
        /// <param name="tipTitle">气泡标题</param>
        /// <param name="tipText">气泡文本内容</param>
        /// <param name="icon">气泡图标</param>
        public void SetNotify(string tipTitle, string tipText, ToolTipIcon icon)
        {
            ((ShopSoftMain)Application.OpenForms[0]).SetNotify(tipTitle, tipText, icon);
        }

        /// <summary>
        /// 获取日期时间区间列表
        /// </summary>
        /// <param name="dateTimeStart">起始时间列表</param>
        /// <param name="dateTimeEnd">终止时间列表</param>
        /// <returns></returns>
        public List<DateTime?[]> GetDateTimeIntervalList(DateTime[] dateTimeStart, DateTime[] dateTimeEnd)
        {
            var list = new List<DateTime?[]>();
            if (dateTimeStart.Length == 2 && dateTimeStart.Length == dateTimeEnd.Length)
            {
                for (int i = 0; i < dateTimeStart.Length; i++)
                {
                    var ls = new DateTime?[2];
                    ls[0] = dateTimeStart[i] == ClientParameterSettings.InvalidDateTime
                               ? (DateTime?)null
                               : dateTimeStart[i];
                    ls[1] = dateTimeEnd[i] == ClientParameterSettings.InvalidDateTime
                               ? (DateTime?)null
                               : dateTimeEnd[i].AddDays(1);
                    list.Add(ls);
                }
            }
            return list;
        }

        /// <summary>
        /// 设置指定控件下所有子控件是否显示
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="isVisible">是否显示</param>
        public void SetAllControlsVisible(Control control, bool isVisible)
        {
            for (int i = 0; i < control.Controls.Count; i++)
            {
                control.Controls[i].Visible = isVisible;
            }
        }

        /// <summary>
        /// 设置可为空的DateTime属性的值
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public string GetDateTimeCanNull(DateTime? dateTime)
        {
            return dateTime == null ? "" : ((DateTime)dateTime).ToShortDateString();
        }

        /// <summary>
        /// 设置可为空的包括时间的DateTime属性的值
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public string GetDateTimeOfTimeCanNull(DateTime? dateTime)
        {
            return dateTime == null ? "" : ((DateTime)dateTime).ToString("yyyy-MM-dd hh:mm:ss");
        }

        /// <summary>
        /// 设置可为空的Double属性的值
        /// </summary>
        /// <param name="doubleValue">double类型的值</param>
        /// <returns></returns>
        public string GetDoubleCanNull(Double? doubleValue)
        {
            return doubleValue == null ? "" : ((Double)doubleValue).ToString();
        }

        /// <summary>
        /// 设置可为空的Double属性的值
        /// </summary>
        /// <param name="doubleValue">double类型的值</param>
        /// <param name="defaultValue">doubleValue为空时的替换值</param>
        /// <returns></returns>
        public string GetDoubleCanNull(Double? doubleValue, string defaultValue)
        {
            return doubleValue == null ? defaultValue : ((Double)doubleValue).ToString();
        }

        /// <summary>
        /// 设置可为空的保留小数的Double属性的值
        /// </summary>
        /// <param name="doubleValue">double类型的值</param>
        /// <param name="value">doubleValue为空时的替换值</param>
        /// <returns></returns>
        public string GetDoubleOfPointCanNull(Double? doubleValue, string value)
        {
            return doubleValue == null ? "" : ((Double)doubleValue).ToString(value);
        }

        /// <summary>
        /// 设置可为空的Int属性的string值
        /// </summary>
        /// <param name="intValue">int值</param>
        /// <returns></returns>
        public string GetIntCanNull(int? intValue)
        {
            return intValue == null ? "" : ((int)intValue).ToString();
        }

        /// <summary>
        /// 设置可为空的Int属性的int值
        /// </summary>
        /// <param name="intValue">int值</param>
        /// <returns></returns>
        public int GetIntCanNullToInt(int? intValue)
        {

            return intValue == null ? 0 : int.Parse(intValue.ToString());
        }

        /// <summary>
        /// 将图片转换为二进制
        /// Add By Bruce 2014-06-04
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public byte[] ConvertJPGToBytes(string fileName)
        {
            Image image = Properties.Resources.Folders48;
            ImageFormat bf = ImageFormat.Jpeg;
            MemoryStream ms = new MemoryStream();
            image.Save(ms, bf);
            byte[] result = new byte[ms.Length];
            ms.Position = 0L;
            ms.Read(result, 0, result.Length);
            ms.Close();
            return result;
        }
        #endregion Method
    }
}
